/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public abstract class FormAbstractHtml extends FormAbstract {

    /**
     *
     * @param request
     * @param nomParam
     * @return
     */
    protected Integer getIntegerFromRequest(HttpServletRequest request, String nomParam) {

        return this.getIntegerFromRequest(request, nomParam, null);
    }

    /**
     *
     * @param request
     * @param nomParam
     * @param defaultValue
     * @return
     */
    protected Integer getIntegerFromRequest(HttpServletRequest request, String nomParam, Integer defaultValue) {

        Integer result;

        try {
            result = Integer.parseInt(request.getParameter(nomParam));
        } catch (NumberFormatException e) {
            result = defaultValue;
        } catch (NullPointerException e) {
            result = defaultValue;
        }
        return result;
    }

    /**
     *
     * @param request
     * @param nomParam
     * @return
     */
    protected Boolean getBooleanFromRequest(HttpServletRequest request, String nomParam) {


        Boolean result;

        try {
            result = Boolean.parseBoolean(request.getParameter(nomParam));
        } catch (NullPointerException e) {
            result = false;
        }
        return result;
    }

    /**
     *
     * @param request
     * @param nomCollection
     * @return
     */
    protected ArrayList<Integer> getArrayListIntegerFromRequest(HttpServletRequest request, String nomCollection) {

        ArrayList<Integer> result = new ArrayList<Integer>(10);

        int num = 0;

        while (true) {
            Integer element = this.getIntegerFromRequest(request, nomCollection + "_" + num);
            if (element != null && element != -1) {
                result.add(element);
                num++;
            } else {
                break;
            }
        }
        result.trimToSize();
        return result;
    }

    /**
     *
     * @param request
     * @param nomCollection
     * @return
     */
    protected ArrayList<String> getArrayListStringFromRequest(HttpServletRequest request, String nomCollection) {

        ArrayList<String> result = new ArrayList<String>(10);

        int num = 0;

        while (true) {
            String element = request.getParameter(nomCollection + "_" + num);
            if (element != null) {
                result.add(element);
                num++;
            } else {
                break;
            }
        }
        result.trimToSize();
        return result;
    }

    /**
     *
     * @param request
     * @param nomParam
     * @return
     */
    protected Float getFloatFromRequest(HttpServletRequest request, String nomParam) {

        Float result;

        try {
            result = Float.parseFloat(request.getParameter(nomParam));
        } catch (NumberFormatException e) {
            result = null;
        } catch (NullPointerException e) {
            result = null;
        }
        return result;
    }

    /**
     *
     * @param request
     * @param nomCheckBox
     * @return
     */
    protected Boolean getCheckBoxFromRequest(HttpServletRequest request, String nomCheckBox) {
        String requestValue = request.getParameter(nomCheckBox);
        if (requestValue != null && requestValue.equals("on")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @param request
     * @return
     */
    protected String getNumSS(HttpServletRequest request) {
        return new StringBuilder(request.getParameter("numss")).append(request.getParameter("cleNumss")).toString();
    }

    /**
     *
     * @param request
     * @param nomParam
     * @return
     */
    protected String getParameterNonNullFromRequest(HttpServletRequest request, String nomParam) {
        String result = request.getParameter(nomParam);
        if (result != null && !result.isEmpty()) {
            return result;
        } else {
            return "";
        }
    }

    /**
     *
     * @param request
     * @param nomPart
     * @return
     */
    protected String getStringFromMultiPartRequest(HttpServletRequest request, String nomPart) {
        try {
            return new Scanner(this.getPartFromRequest(request, nomPart).getInputStream()).nextLine();
        } catch (IOException e) {
            return null;
        } catch (NullPointerException e) {
            return null;
        }
    }

    /**
     *
     * @param request
     * @param nomPart
     * @return
     */
    protected Part getPartFromRequest(HttpServletRequest request, String nomPart) {
        try {
            return request.getPart(nomPart);
        } catch (IOException e) {
            return null;
        } catch (ServletException e) {
            return null;
        }
    }

    /**
     *
     * @param request
     * @param nomParam
     * @param defaultValue
     * @return
     */
    protected Short getShortFromRequest(HttpServletRequest request, String nomParam, Short defaultValue) {
        try {
            return Short.parseShort(request.getParameter(nomParam));
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     *
     * @param request
     * @param nomParam
     * @param defaultValue
     * @return
     */
    protected Byte getByteFromRequest(HttpServletRequest request, String nomParam, Byte defaultValue) {
        try {
            return Byte.parseByte(request.getParameter(nomParam));
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     *
     * @param request
     * @param parameterName
     * @return
     */
    protected String getNonNullStringFromRequest(HttpServletRequest request, String parameterName) {
        return (request.getParameter(parameterName) != null ? request.getParameter(parameterName) : "");
    }

    /**
     *
     * @param request
     * @param nomParam
     * @param datePattern
     * @return
     */
    protected Date getDateFromRequest(HttpServletRequest request, String nomParam, String datePattern) {
        try {
            return new SimpleDateFormat(datePattern).parse(request.getParameter(nomParam));
        } catch (ParseException e) {
            return null;
        }
    }
}
